//
//  JokerManager.m
//  FiveWork
//
//  Created by lanou3g on 15/11/23.
//  Copyright © 2015年 Project. All rights reserved.
//

#import "JokerManager.h"
#import "JokerModel.h"
#import <AFNetworking.h>
#define JokerUrl @"http://c.3g.163.com/recommend/getChanListNews?passport=&devId=861138021842824&size=20&channel=duanzi"
@interface JokerManager ()
@property(nonatomic,strong)NSMutableArray *jokerArray;

@end

@implementation JokerManager


/**
 *  单例
 */
static JokerManager *jokerManger =nil;
+(instancetype)defaultManger{
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        jokerManger =[[JokerManager alloc] init];
    });
    return jokerManger;
}
/**
 *  懒加载
 *
 *  @return <#return value description#>
 */
-(NSMutableArray*)jokerArray{
    
    if (_jokerArray ==nil) {
        _jokerArray =[NSMutableArray array];
    }
    return _jokerArray;
    
}






/**
 *  解析数据
 */
-(void)requestDataWithHandle:(void(^)())handle YesOrNo:(BOOL)YesOrNo{
    
    AFHTTPRequestOperationManager *manger =[[AFHTTPRequestOperationManager alloc] init];
    manger.responseSerializer.acceptableContentTypes =[NSSet setWithObject:@"application/json"];
    [manger GET:JokerUrl parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (YesOrNo ==YES) {
            
                    }else{
                        //清空数组
                        [self.jokerArray removeAllObjects];

        }
        NSDictionary *dict =[NSDictionary dictionaryWithDictionary:responseObject];
        NSArray *arr =dict[@"段子"];
        for (NSDictionary *dic in arr) {
            JokerModel *model =[[JokerModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.jokerArray addObject:model];
            
        }
        handle();
     
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        NSLog(@"解析失败");
    }];
    

    
}

/**
 *  返回数组
 */
-(NSArray *)getJokerArray{
    return self.jokerArray;
}

/**
 *  返回model
 */
-(JokerModel *)getJokerModelWithIndex:(NSInteger)index{
    
    return self.jokerArray[index];
}




@end
